/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////Author: Shahab.Alishahian@gmail.com - SA	
///////////////Target:The following methods are here to initial the plugin.
///////////////Version:1.1
///////////////CopyRight: 2014 Noranj Co.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//*************************************************************************************************************
//******Facebook SDK setting

var appId='' ;
var accessToken='' ;
var appAccessToken='' ;
var channelUrl='' ;

if(window.location.href.indexOf("appspot") > -1){
	//JCOMMENT
	appId ='465055323603232' ;
	accessToken ='' ;
	appAccessToken ='' ;
	channelUrl ='localhost' ;
}
else{
	 //JBCLIENT
	appId='1384343251799270' ;
	accessToken='CAATrDYfFTOYBAIaqIKrdQIEskTtYCYPOJiChsmaK67diT24JazrgriZAGNfnxQ0SyqjiyHrwwBpGZC77WZBCyXSGndzVKixAiI4Urk56Q2TkvkHpM4LiHLUdoGUo6xJGZB3iuB9ZCXshaLpzBH2HcZAWB5h6QZAx55dTIVZBLrLjZCvLph7eMHjKPgb1cwBcTr2ml6JEXvfZAiuwZDZD' ;
	appAccessToken='1384343251799270|YRd1ffbp0JaVVHTJKOlSUmWan_Q' ;
	channelUrl='localhost' ;
}


//*************************************************************************************************************
var clientId = '913666381572.apps.googleusercontent.com'; //jb-shhb->'828062657182-a2i7dlpvhlv2uqe4f101i1430qh1thmr.apps.googleusercontent.com';
var apiKey = 'AIzaSyBrR0uxe3x6G2-ntujf9YURrfE5R9QT728'; //jb-shhb->'AIzaSyCZtTH-e9ZXKkNX07j7RzTtDttNS_JrwfQ';
var scopes = 'https://www.googleapis.com/auth/userinfo.email'; // 'https://www.googleapis.com/auth/plus.me';
var MY_API_ROOT = 'https://shhbjb.appspot.com/_ah/api';
var MY_API_VER = 'v4';
var MY_API_NAME = 'jb';


//$.getScript('https://apis.google.com/js/client.js', function() {        
//	  $('body').append('<p>GOOGLE API  is loaded</p>');
//});

window.onload = function() {
    //gapi.client.setApiKey(apiKey);
    gapi.client.load(MY_API_NAME, MY_API_VER, null, MY_API_ROOT);
}

//Finding javascript plugin and get the merchantId 
var a=document.getElementById("jjjavascript");
var GB_merchantId = a.getAttribute("parammerchantid");

$(document).ready(function() {
	$('#jcImageUrl').val($("#ProductImage").attr("src")); //getQueryStringParams('imageurl')
	$('#jcPageUrl').val(window.location.href); //getQueryStringParams('pageurl')
	$('#jcMerchantId').val(GB_merchantId); //getQueryStringParams('merchantid')
	//Load the Facebook SDK async.
	window.fbAsyncInit = function () {
	    FB.init({
	        appId: appId, // App ID
	        channelUrl: channelUrl, // Channel File
	        status: true, // check login status
	        cookie: true, // enable cookies to allow the server to access the session
	        xfbml: true, // parse XFBML
	        frictionlessRequests : true
	    });
	    //If the use was authenticated does not need to re-login.
	    FB.getLoginStatus(function (response) {
		    if (response.status === 'connected') {
		    	$("#jcFbLoginDiv").hide();
		    	$("#jcOpenComment").show();
		    }
		    else{
		    	$("#jcFbLoginDiv").show();
		    	$("#jcOpenComment").hide();
		    }

		});
		
	};
	// Load the SDK Asynchronously
	(function (d) {
	    var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
	    if (d.getElementById(id)) {
	        return;
	    }
	    js = d.createElement('script');
	    js.id = id;
	    js.async = true;
	    js.src = "//connect.facebook.net/en_US/all.js";
	    ref.parentNode.insertBefore(js, ref);
	} (document));

  	$('.imageMeRef').each(function(){
        $(this).attr("src",  'images/unperson.jpg');
    });
  	
  	$("#jcOpenComment").click(function(){
  			$('.composer').fadeIn(1000, function() {
  				if($("#jcBoardId").val()=='0' || $("#jcBoardId").val()=='' ){
  					loggedin();
  				}
  			});
  		});
  	
  	$("#jcAddComment").click(function(){
  		addComment();
  		$('.composer').fadeOut(1000, function() {
  			$('#jcFadedDiv').fadeIn(1000).css("display", "block");
  		});
  		//$("#boardCommentList").empty();
  		addAConversation();
  	});
  	
  	$("#jcBoardId").change(function(){
  		loadConversations($(this).val());
  	});
  	
  	 $("#btn").click(
  			function(){
  		    	var finalresult;
  		    	$.ajax({
  		    		type: 'GET',
  			    	url:  window.location.pathname,
  			        success : function(result)
  			        {
  			        	var response = result;
  			        	finalresult = $('meta[name="Description"]').attr('content');
  			        	$("#contentHolder").prepend(finalresult);
  			        },
  			        error: function(error) {
  			           	   console.log(error.status);
  			           }
  		    	});
  		    });
  	
  	 $("#btnShowContent").click(
   			function (){
   				$.get("http://ipinfo.io", function (response) {
   					$("#contentHolder").prepend("IP: " + response.ip);
   					$("#contentHolder").prepend("Location: " + response.city + ", " + response.region);
   					$("#contentHolder").prepend(JSON.stringify(response, null, 4));
   				}, "jsonp");
   		 });
  	
  	 var myVar = setTimeout(function(){
  		getSYSClientInfo();
	    getOwnerUserId($("#jcMerchantId").val());
	    loadBoard($('#jcPageUrl').val());
  	  },3000);
  	
  	
});
